# -*- coding:utf-8 -*-

# @File     :visual_model
# @Author   :Yupeng
# @Date     :2019/2/26 15:28

'''
用于图像识别，表示训练任务生成的模型
'''

from django.db import models
from django.contrib.postgres.fields import ArrayField, JSONField

from .task import VisualRecognitionTask
from apps.accounts.models import User


class VisualRecognitionModel(models.Model):
    '''
    classes：各个类的名字
    classes_images:各个类的名字和每个类中图片的张数组成的字典的列表
    result：对应于VisualRecognitionTask中的train_result
    '''
    title = models.CharField(max_length=254, default='Default model')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='模型生成时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='模型最后更新时间')
    # number_of_classes = models.IntegerField(null=True)
    # number_of_images = models.IntegerField(null=True)
    # classes = ArrayField(models.CharField(max_length=254), default=None, blank=True, null=True)
    # images_of_classes = ArrayField(models.IntegerField(null=True))
    classes_images = ArrayField(models.CharField(max_length=50), default=None, null=True)
    description = models.CharField(max_length=254, null=True)
    # path = models.FilePathField(null=True)
    path = JSONField(models.CharField(max_length=254), null=True)  # 各个模型文件的路径
    # project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='project_model')
    vrtask = models.ForeignKey(VisualRecognitionTask, on_delete=models.CASCADE, related_name='vrtask_model', verbose_name='生成该模型的训练任务')
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_vr_model')
    result = JSONField(models.CharField(max_length=254), null=True)

    class Meta:
        ordering = ('-update_time',)

    def to_dict(self):
        '''
        返回字段dict
        :return:
        '''
        d = {
            'id': self.id,
            'title': self.title,
            'created_time': self.created_time,
            'update_time': self.update_time,
            'classes_images': self.classes_images,
            'description': self.description,
            'path': self.path,
            'result': self.result
        }
        return d
